package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tanhua.dubbo.mappers.UserMapper;
import com.tanhua.model.domain.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author: cl
 * @Date: 2022/4/30 - 04 - 30
 * @Description:
 */
@DubboService
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;

    /**
     * 通过手机号查找用户
     *
     * @param mobile
     * @return
     */
    @Override
    public User findByMobile(String mobile) {
        User user = userMapper.selectOne(
                Wrappers.<User>lambdaQuery()
                        .eq(User::getMobile, mobile)
        );
        return user;
    }


    /**
     * 登录(保存新用户)
     *
     * @param user
     * @return
     */
    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    /**
     * 修改用户信息
     *
     * @param user
     */
    @Override
    public void update(User user) {

        userMapper.updateById(user);
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @Override
    public List<User> findAll() {
        List<User> users = userMapper.selectList(
                Wrappers.<User>lambdaQuery()
        );
        return users;
    }

    /**
     * 通过id查询用户
     *
     * @param userId
     * @return
     */
    @Override
    public User findById(Long userId) {
        User user = userMapper.selectById(userId);
        return user;
    }

    /**
     * 通过环信id查询用户
     *
     * @param huanxinId
     * @return
     */
    @Override
    public User findByHuanXinId(String huanxinId) {
        User user = userMapper.selectOne(
                Wrappers.<User>lambdaQuery()
                        .eq(User::getHxUser, huanxinId)
        );
        return user;
    }

    /**
     * 获取用户总数
     *
     * @return
     */
    @Override
    public Long countAllUser() {
        Integer integer = userMapper.selectCount(null);
        return Long.valueOf(integer);
    }
}
